import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as patches
from matplotlib.path import Path

# Set up the figure
fig, ax = plt.subplots(figsize=(8, 8))

# Create a square with side length 2
square_side = 2
square = plt.Rectangle(
    (-square_side / 2, -square_side / 2),
    square_side,
    square_side,
    fill=True,
    color="blue",
    alpha=0.3,
    label="Square",
)

# Create a circle inside the square (radius = half the square's side length)
circle_radius = square_side / 2
circle = plt.Circle(
    (0, 0), circle_radius, fill=True, color="red", alpha=0.3, label="Circle"
)

# Add shapes to the plot
ax.add_patch(square)
ax.add_patch(circle)

# Calculate and display the areas
square_area = square_side**2
circle_area = np.pi * (circle_radius**2)
difference = square_area - circle_area

# Add text explaining the area difference
ax.text(0, -1.5, f"Square area = {square_area:.2f}", ha="center", fontsize=12)
ax.text(0, -1.7, f"Circle area = {circle_area:.2f}", ha="center", fontsize=12)
ax.text(0, -1.9, f"Difference = {difference:.2f}", ha="center", fontsize=12)
ax.text(
    0,
    -2.1,
    f"Ratio (circle/square) = {circle_area/square_area:.2f}",
    ha="center",
    fontsize=12,
)

# Set the axis limits and labels
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-2.3, 1.5)
ax.set_aspect("equal")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.grid(True)
ax.set_title("Area Difference: Square vs Inscribed Circle")
ax.legend(loc="upper right")

# Show the plot
plt.tight_layout()
plt.savefig("square_inscribed_circle_comparison.png")
plt.show()
